﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrbaBus.CompraProducto;
using FrbaBus.ConsultaPuntos;
using FrbaBus.Properties;
using System.Data.SqlClient;

namespace FrbaBus.IngresoCliente
{
    public partial class FormIngresarNuevoCliente : Form
    {
        private string referer;

        public FormIngresarNuevoCliente(string referer)
        {
            InitializeComponent();
            CargarCombos();
            this.ActiveControl = tbNombre;
            tbDNI.Text = Cliente.clientId;
            this.referer = referer;
        }

        private void CargarCombos()
        {

            cbSexo.Items.Insert(0, "M");
            cbSexo.Items.Insert(1, "F");
            this.cbSexo.SelectedIndex = 0;


            cbDiscapacitado.Items.Insert(0, "No");
            cbDiscapacitado.Items.Insert(1, "Si");
            this.cbDiscapacitado.SelectedIndex = 0;
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {

            
            string sexo =  this.cbSexo.Items[this.cbSexo.SelectedIndex].ToString();

            int discapacitado = this.cbDiscapacitado.SelectedIndex;
            
            long telefono = Convert.ToInt64(tbTelefono.Text.Trim().ToString());

            string sConnectionString = Settings.Default.GD1C2013ConnectionString;
            SqlConnection connection = new SqlConnection(sConnectionString);
            SqlCommand cmd = new SqlCommand("THE_BEST_IAS.pr_alta_cliente", connection);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@p_nombre", SqlDbType.VarChar)).Value = tbNombre.Text.Trim();
            cmd.Parameters.Add(new SqlParameter("@p_apellido", SqlDbType.VarChar)).Value = tbApellido.Text.Trim();
            cmd.Parameters.Add(new SqlParameter("@p_dni", SqlDbType.BigInt)).Value = Convert.ToInt64(tbDNI.Text.Trim());
            cmd.Parameters.Add(new SqlParameter("@p_sexo", SqlDbType.VarChar)).Value = sexo;
            cmd.Parameters.Add(new SqlParameter("@p_fecha_nacmiento", SqlDbType.DateTime)).Value = Convert.ToDateTime(dtpFechaNac.Text.ToString().Trim());
            cmd.Parameters.Add(new SqlParameter("@p_direccion", SqlDbType.VarChar)).Value = tbDireccion.Text.Trim();
            cmd.Parameters.Add(new SqlParameter("@p_telefono", SqlDbType.BigInt)).Value = telefono;
            cmd.Parameters.Add(new SqlParameter("@p_mail", SqlDbType.VarChar)).Value = tbEmail.Text.Trim();
            cmd.Parameters.Add(new SqlParameter("@p_discapacitado", SqlDbType.Int)).Value = discapacitado;

            //cmd.Parameters.Add("@returnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.CommandTimeout = 1000;

            connection.Open();
            cmd.ExecuteNonQuery();

            //int valorRetorno = Convert.ToInt32(cmd.Parameters["@returnValue"].Value);

            connection.Close();

            //if (valorRetorno == 0)
            //{
            //}

            this.Hide();

            if (this.referer == "compra")
            {
                FormCompraProducto form = new FormCompraProducto(long.Parse(tbDNI.Text.Trim()));
                form.ShowDialog(this);
            }
            if (this.referer == "consulta")
            {
                FormConsultarPuntos form = new FormConsultarPuntos();
                form.ShowDialog(this);
            }
            if (this.referer == "canje")
            {

            }
        }
    }
}
